package com.cykj.page02;

import java.io.FileInputStream;

/**
 * @BelongsProject: MyTomcat
 * @BelongsPackage: com.cykj.page03
 * @Author: WWJ
 * @CreateTime: 2025-06-23 22:14
 * @Description: TODO
 * @Version: 1.0
 */
public class StaticResourceHandler {
    private String filePath;
    private byte[] fileBytes;
    private String media;

    public StaticResourceHandler(String filePath) {
        this.filePath = filePath;
        getFileByte();
        getFileMedia();
    }

    private void getFileByte() {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            fileBytes = new byte[fis.available()];
            fis.read(fileBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //可优化
    //获取文件的媒体类型
    private void getFileMedia(){
        String[] split = filePath.split("\\.");
        //后缀
        String suffix = split[split.length - 1];
        if (suffix.equals("html")){
            media = "text/html";
        }else if (suffix.equals("png")){
            media = "image/png";
        }else if (suffix.equals("jpg")){
            media = "image/jpeg";
        }
    }


    public byte[] getFileBytes() {
        return fileBytes;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getMedia() {
        return media;
    }
}
